package com.legendshop.oa.constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.legendshop.util.AppUtils;
import com.legendshop.util.EnvironmentConfig;

/**
 * 附件类型定义 目前支持视频和图片
 * 
 */
public class AttachmentDefinitionFactory {

	private static AttachmentDefinitionFactory instance = null;

	private  Map<ImageTypeEnum, AttachmentDefinition> attachmentList;

	public static AttachmentDefinitionFactory getInstance() {
		if (instance == null) {
			synchronized (AttachmentDefinitionFactory.class) {
				instance = new AttachmentDefinitionFactory();
				instance.init();//初始化
			}
		}
		return instance;
	}
	
	public void init(){
		attachmentList = new HashMap<ImageTypeEnum, AttachmentDefinition>();
		for (ImageTypeEnum type : ImageTypeEnum.values()) {
			if (ImageTypeEnum.IMG.equals(type)) {// 图片
				String allowType = EnvironmentConfig.getInstance().getPropertyValue(CommonConfigPropertiesEnum.IMG_ALLOW_TYPE); // 允许上传的文件类型
				String allowSize = EnvironmentConfig.getInstance().getPropertyValue(CommonConfigPropertiesEnum.IMG_ALLOW_SIZE); // 允许上传的文件大小

				Long maxUploadSize = null;
				List<String> allowUploadType = new ArrayList<String>();

				if (allowSize != null) {
					try {
						maxUploadSize = Long.parseLong(allowType) * 1024; // 以K为单位
					} catch (Exception e) {
						maxUploadSize = new Long(1024 * 1024); // 默认允许上传的大小是1M
					}
				} else {
					maxUploadSize = new Long(1024 * 1024); // 默认允许上传的大小是1M
				}

				if (allowType != null) {
					String[] types = allowType.split(",");
					if (types != null) {
						for (String t : types) {
							allowUploadType.add(t);
						}
					}
				} else {// 默认值
					allowUploadType.add(".jpg");
					allowUploadType.add(".jpeg");
					allowUploadType.add(".png");
					allowUploadType.add(".bmp");
					allowUploadType.add(".gif");
				}

				AttachmentDefinition ad = new AttachmentDefinition(type.value(), maxUploadSize, allowUploadType);
				attachmentList.put(type, ad);
			} else if (ImageTypeEnum.VIDEO.equals(type)) {// 视频
				String allowType = EnvironmentConfig.getInstance().getPropertyValue(CommonConfigPropertiesEnum.VIDEO_ALLOW_TYPE); // 允许上传的文件类型
				String allowSize = EnvironmentConfig.getInstance().getPropertyValue(CommonConfigPropertiesEnum.VIDEO_ALLOW_SIZE); // 允许上传的文件大小

				Long maxUploadSize = null;
				List<String> allowUploadType = new ArrayList<String>();

				if (allowSize != null) {
					try {
						maxUploadSize = Long.parseLong(allowType) * 1024; // 以K为单位
					} catch (Exception e) {
						maxUploadSize = new Long(5120 * 1024); // 默认允许上传的大小是5M
					}
				} else {
					maxUploadSize = new Long(5120 * 1024); // 默认允许上传的大小是5M
				}

				if (allowType != null) {
					String[] types = allowType.split(",");
					if (types != null) {
						for (String t : types) {
							allowUploadType.add(t);
						}
					}
				} else {// 默认值
					allowUploadType.add(".mp4");
					allowUploadType.add(".webm");
					allowUploadType.add(".mpeg");
					allowUploadType.add(".ogg");
				}

				AttachmentDefinition ad = new AttachmentDefinition(type.value(), maxUploadSize, allowUploadType);
				attachmentList.put(type, ad);
			}
		}
	}

	public AttachmentDefinition getAttachmentDefinitionByType(ImageTypeEnum type) {
		return attachmentList.get(type);
	}
	/**
	 * 根据后缀获取对应的文件类型定义
	 * @param extName
	 * @return
	 */
	public AttachmentDefinition getAttachmentDefinitionByType(String extName) {
		if(AppUtils.isBlank(extName)){
			return null;
		}
		for (AttachmentDefinition ad : attachmentList.values()) {
			if(ad.getAllowUploadType().contains(extName)){
				return ad;
			}
		}
		return null;
	}

	public Map<ImageTypeEnum, AttachmentDefinition> getAttachmentList() {
		return attachmentList;
	}
	
}
